/***************************************************************************
 *   Copyright (C) 2008 by Milambert									   *
 *   nexus6b@gmail.com   												   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <mysql/mysql.h>
#include "config.h"
#include "common.h"
#include "confHandler.h"
#include "mysqlHandler.h"

MYSQL *mysql;

void mysqlInit(void){
	mysql = NULL;
	writeLog("II:	Connection to MySQL");
	
	if ((mysql = mysql_init(mysql)) == NULL){
		writeLog("EE:	echec mysql_init");
		exit(EXIT_FAILURE);
	}
	
	//connection mysql
	if (!mysql_real_connect(mysql, myHost, myUser, myPwd, myDB, myPort, NULL, 0)){
		writeLog("EE:	echec mysql_real_connect");
        writeLog("      MySQL error:\n");
        writeLog((char *)mysql_error(mysql));
        writeLog("\n");
		exit(EXIT_FAILURE);
	}
	
	writeLog("II:	MySQL connected");
}

void closeMysql(void){
	writeLog("II:	Deconnection from MySQL");
	
	//mysql_free_result(result);
	mysql_close(mysql);
	
	writeLog("II:	MySQL closed");
}

MYSQL_RES * mysqlGet(char *query){
    MYSQL_RES *result;
    
#ifdef DEBUG
    printf("\n%s\n\n", query);
#endif
    
    //check is connection is alive
    if(mysql_ping(mysql) > 0){
    	writeLog("EE:	Connection with MySQL lost");
        writeLog("      MySQL error:\n");
        writeLog((char *)mysql_error(mysql));
        writeLog("\n");
        exit(EXIT_FAILURE);
    }
    
    if (mysql_query(mysql, query)){
        writeLog("EE:   echec query");
        writeLog("      MySQL error:\n");
        writeLog((char *)mysql_error(mysql));
        writeLog("\n");
        exit(EXIT_FAILURE);
    }
        
    if (!(result = mysql_store_result(mysql))){
        writeLog("EE:   echec mysql_store");
        writeLog("      MySQL error:\n");
        writeLog((char *)mysql_error(mysql));
        writeLog("\n");
        exit(EXIT_FAILURE);
    }

    return result;
}

int mysqlFetch(char *query){
    int nb;
    
#ifdef DEBUG
    printf("\n%s\n\n", query);
#endif
    
    //check is connection is alive
    if(mysql_ping(mysql) > 0){
    	writeLog("EE:	Connection with MySQL lost");
        writeLog("      MySQL error:\n");
        writeLog((char *)mysql_error(mysql));
        writeLog("\n");
        exit(EXIT_FAILURE);
    }
    
    //send request to mysql
    if (mysql_query(mysql, query)){
        writeLog("EE:   echec query");
        writeLog("      MySQL error:\n");
        writeLog((char *)mysql_error(mysql));
        writeLog("\n");
        exit(EXIT_FAILURE);
    }
    
    /*if((nb = mysql_affected_rows(mysql)) == 0){
        writeLog("EE:   echec writing to MySQL");
        exit(EXIT_FAILURE);
    }*/
    
    return nb;
}
